let Controller = {
    mounted() {
        this.initData();
    },
    activated() {
        this.initData();
        this.requestDetail();
        this.onDownloadExcel();
    },
    methods: {
        initData() {
            const param = JSON.parse(this.$route.query.data);
            this.Model.queryData = _.cloneDeep(param);
            this.Model.Outofstock = param.flag;
            this.Model.OutofstockBtn = param.status;
            this.Model.MainId = param.id;		//订单id
        },
        onUploadFileChange(e) {
            if (e.target.files.length == 0) {
                return;
            }
            this.Model.uploadFile = e.target.files[0];
        },
        onDownloadExcel() {
            this.$ajax.get(this.apiTest + `/supplier/whWarehouseDetail/getGoodsExcelInfo`)
                .then(res => {
                    const data = res.data;
                    if (data.status == 200) {
                        this.Model.fileUrl = data.result.fileUrl;
                        this.Model.fileName = data.result.fileName;
                    }
                })
        },
        onEditCount(index) {
            let id = this.Model.detailData[index].id;
            if (!id) {
                this.Model.detailData[index].id = 0;
                return;
            }
            this.Model.detailData[index].id = parseFloat(id).toFixed(1);
        },
        getFilterData(val) {
            this.Model.searchFromData = val;
            console.log(this.Model.searchFromData)
        },
        requestDetail() {
            const whInventoryMainId = this.Model.queryData.id;
            const param = {
                condition: {
                    whInventoryMainId
                }
            }
            if (whInventoryMainId) this.$ajax
                .post(`/api/whInventoryDetails/list`, param)
                .then(response => {
                    const data = response.data;
                    if (data.status == 200) {
                        const result = data.result;
                        this.Model.detailData = result.detailsList;
                        this.Model.filterOption.items[0].data = result.countingNumber;
                        this.Model.filterOption.items[1].data = result.sapCountingNumber;
                        this.Model.filterOption.items[2].data = result.inventoryWarehouse;
                        this.Model.filterOption.items[3].data = result.createBy;
                        this.Model.filterOption.items[4].data = result.inventoryStartTime;
                        this.Model.hasBeentotal = result.hasBeentotal;
                        this.Model.totalNumber = result.total;
                    } else {
                        this.$message.error(data.msg);
                    }

                })
                .catch(error => {
                    this.$message.error('请求错误');
                });
        },
        //请求扫码入库 
        skuEntryAjax(that, code) {
            var fiNum = this.Model.hasBeentotal;
            this.$ajax({
                method: 'post',
                //url:this.apiTest+'/supplier/whInventoryDetails/accordingToBarcodeComparison'+code,
                url: '/api/whInventoryDetails/accordingToBarcodeComparison/'+ fiNum,
                data: {
                    keyword: code,
                    whInventoryMainId: this.Model.MainId
                }
            }).then((res) => {
                var data = res.data;
                if (data.status == 200) {
                    var result = data.result;
                    if (result) {
                        this.Model.hasBeentotal = result.totalNumberOfSolids;
                        var dataArr = [];
                        this.Model.tableData.forEach((val, idx, arr) => {
                            //已有的条码
                            if (val.lotNumber == code) {
                                dataArr.push(val.lotNumber);
                                if (dataArr.length > 0) {
                                    this.Model.tableData.splice(idx, 1);
                                    this.Model.tableData.unshift(result.inventoryDetails);
                                } else {
                                    console.log('无条码')
                                    this.Model.tableData.push(result.inventoryDetails);
                                }
                            }
                        });
                    }
                } else {

                }

            });
        },

        //扫码入库
        checkSku() {
            var code = this.Model.writeSku;
            this.skuEntryAjax(this, code);
        },
        //保存、结束盘点
        inventoryHandle(type) {
            var mainTableData = this.Model.detailData;
            var Nonumberarr = '';//数量小于0的字符串
            var arr = [];
			mainTableData.forEach(item=>{
				if(item.numberOfSolidOrders <= 0){
					Nonumberarr += item.goodsNumber+'<br/>';
                }else{
                    var obj = {
                        id: item.id,
                        countingResults: item.countingResults,
                        numberOfDiscs: item.numberOfDiscs,
                        numberOfSolidOrders: item.numberOfSolidOrders,
                        whInventoryMainId: this.Model.MainId,
                        remark: item.remark
                    }
                    arr.push(obj);
                }
			});
            if (type == '结束盘点') {
                if(!Nonumberarr==''){
					this.$alert('<h4>以下商品编码未填写应盘数量</h4><p>'+Nonumberarr+'</p>', '提示', {
						dangerouslyUseHTMLString: true
					});
				}else{
					this.inventoryAjax(arr, 1, '已结束盘点');
				}
            } else {
                if(!Nonumberarr==''){
					this.$alert('<h4>以下商品编码未填写应盘数量</h4><p>'+Nonumberarr+'</p>', '提示', {
						dangerouslyUseHTMLString: true
					});
				}else{
					this.inventoryAjax(arr, 0, '保存成功');
				}  
            }

        },
        //保存、结束盘点请求
        inventoryAjax(params, whetherToEnd, word) {
            var Solids = this.Model.fiNum;
            this.$ajax({
                method: 'post',
                //url:this.apiTest+'/supplier/whInventoryDetails/saveCountData/'+whetherToEnd,
                url: `/api/whInventoryDetails/saveCountData/${whetherToEnd}`,
                data: params
            }).then((res) => {
                var data = res.data;
                if (data.status == 200) {
                    this.tips(this, 'success', word)
                }
            });
        },
        //提交报告
        submitReport() {
            var submitter = 1;
            var MainId = this.Model.MainId;
            this.$ajax({
                method: 'post',
                //url:this.apiTest+`/supplier/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`,
                url: `/api/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`
            }).then((res) => {
                var data = res.data;
                if (data.status == 200) {
                    this.tips(this, 'success', '提交报告成功')
                }
            });
        },
        //开始盘点
        BegainInventory() {
            this.Model.Outofstock = '0';
            this.Model.OutofstockBtn = '1';
        },
        onTableCurrentChange(val) {
            //表格分页-当前页变动操作
            this.Model.pager.params.pageIndex = val;
            this.requestDetail();
        },
        onTableSizeChange(val) {
            //表格分页-页数量变动操作
            this.Model.pager.params.pageSize = val;
            this.requestDetail();
        },
        onCardClick(color) {
            //页面card搜索点击操作
            this.Model.cardSelected = color;
        },
        onTableSelectionChange(val, index, a) {
            this.Model.exportTableSelectList = val;
        },
        cardClick(color) {
            this.Model.cardSelected = color;
        },
        showChild(val) {
            this.Model.searchFromData = val;
        },
        toSearch(val) {
            console.log(val)
        },
        toClear(val) {
            this.Model.searchFromData = this.Model.defaultSearchFromData;
        },
        toExport(val) {
            console.log(val);
        },
        tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
    }
}
export default Controller;
